import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ResponseInterceptor } from './common/response.interceptor';
//引入swagger
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { useContainer } from 'class-validator';
import validationOptions from './utils/validation-options';
import { ConfigService } from '@nestjs/config';

async function bootstrap() {
	const app = await NestFactory.create(AppModule, { cors: true });
	useContainer(app.select(AppModule), { fallbackOnErrors: true });
	const configService = app.get(ConfigService);

	app.enableShutdownHooks();
	app.setGlobalPrefix(configService.get('app.apiPrefix'), {
		exclude: ['/'],
	});

	app.useGlobalInterceptors(new ResponseInterceptor());

	app.useGlobalPipes(new ValidationPipe(validationOptions));

	const options = new DocumentBuilder().setTitle('API').setDescription('API docs').setVersion('1.0').addBearerAuth().build();

	const document = SwaggerModule.createDocument(app, options);
	SwaggerModule.setup('docs', app, document);

	await app.listen(3000);
}

void bootstrap();
